﻿/*----------------------------------------------------------------
           // Copyright (C) 2008 安华顺通(北京)科技有限公司
           // 版权所有。 
           //
           // 文件名：CurrentUser.cs
           // 文件功能描述：
           //
           // 
           // 创建标识：丁余良 2008 2008-9-8 11:15:21   建立文件
           //
           // 修改标识  //update1  at date...
           // 修改描述：
           //
           // 修改标识：//update2  at date...
           // 修改描述：
//----------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Web;
using System.Windows.Forms;
using Microsoft.Win32;

namespace RuleCode.Common
{
    public class WebUsers
    {
        private static WebUsers instance = new WebUsers();

        public WebUsers() { this.users = new List<CurrentUser>(); }

        public static WebUsers Instance
        {
            get
            {
                if (instance == null)
                    instance = new WebUsers();
                return instance;
            }
        }

        private List<CurrentUser> users;
        public List<CurrentUser> Users
        {
            get { return this.users; }
        }
    }

    public class CurrentUser
    {

        #region 静态属性方法
        public static readonly System.Drawing.Color SelectColor = SystemColors.ActiveCaption;
        public static int runAtServer = 1;// 0:发布；1:运行和设计


        public static string GridGroup = "OverView";

        private static string filePath;
        /// <summary>
        /// 获取或设置 本地文件路径
        /// </summary>
        public static string FilePath
        {
            get
            {
                if (filePath == null || filePath.Trim() == "")
                {
                    RegistryKey rgKey = Registry.CurrentUser.OpenSubKey(@"Software\RuleCode\ConfigurationFilePath", RegistryKeyPermissionCheck.ReadWriteSubTree);
                    if (rgKey == null)
                    {
                        filePath = Application.StartupPath + "\\Data";
                    }
                    else
                    {
                        filePath = rgKey.GetValue("ConfigurationFilePath", Application.StartupPath).ToString();
                    }
                    rgKey.Close();
                }

                return filePath;
            }
        }

        private static string fileName;
        /// <summary>
        /// 获取配置文件名称（不包括文件路径）
        /// </summary>
        public static string FileName
        {
            get
            {
                //if (fileName == null || fileName.Trim() == "")
                //{
                //    RegistryKey rgKey = Registry.CurrentUser.OpenSubKey(@"Software\RuleCode\ConfigurationFilePath", RegistryKeyPermissionCheck.ReadWriteSubTree);
                //    if (rgKey == null)
                //    {
                //        fileName = GetCurrentFileName();
                //    }
                //    else
                //    {
                //        fileName = rgKey.GetValue("SetUpFileName", GetCurrentFileName()).ToString();
                //    }
                //    rgKey.Close();
                //}
                //return fileName;
                fileName = "setup.xml";
                return fileName;
            }
        }

        private static string excutePath;
        /// <summary>
        /// 获取或设置 程序执行路径
        /// </summary>
        public static string ExcutePath
        {
            get
            {
                if (excutePath == null || excutePath == "")
                    excutePath = Application.StartupPath;
                return excutePath;
            }
        }

        ///// <summary>
        ///// 获取或设置当前用户运行的状态，在Form下运行，还是在 Web下运行
        ///// </summary>
        //public static RunState RunState { get; set; }

        //public static CurrentUser Instance
        //{
        //    get
        //    {
        //        if (RunState == RunState.Form)
        //            return FormUser.GetInstance();
        //        else
        //            return WebUser.GetInstance();
        //    }
        //}
        #endregion

        #region 构造函数
        private string DefaultCompanyID = "RUL";

        public CurrentUser()
        {
            //GetConfigurationInfo();
        }
        #endregion

        #region 配置属性
        /// <summary>
        /// 获取或设置 默认公司
        /// </summary>
        public string DefaultCompany { get; set; }

        /// <summary>
        /// 获取或设置 默认用户
        /// </summary>
        public string DefaultUser { get; set; }


        /// <summary>
        /// 获取或设置 当前服务器IP地址
        /// </summary>
        public string ServerIPAddress { get; set; }

        /// <summary>
        /// 获取或设置 服务器端口
        /// </summary>
        public int ServerPort { get; set; }

        /// <summary>
        /// 获取或设置 数据库服务器IP地址
        /// </summary>
        public string DataBaseServerIP { get; set; }
        #endregion

        //#region 当前用户属性

        /////  <summary> 
        ///// 默认公司编码
        /////  </summary> 
        /////  <returns></returns> 
        //public string 默认公司编码 { get; set; }

        //public string DATAAREAID { get; set; }

        //public string CurrentCompanyID
        //{
        //    get
        //    {
        //        if (this.DATAAREAID != "" && this.DATAAREAID != null)
        //            return this.DATAAREAID;
        //        else if (this.默认公司编码 != "" && this.默认公司编码 != null)
        //            return this.默认公司编码;
        //        else
        //            return DefaultCompanyID;
        //    }
        //}

        //public string UserID { get; set; }

        ////丁宁 修改
        //public string UserName { get; set; }
        //private List<string> _Rights = new List<string>();

        ///// <summary>
        ///// 用户权限列表
        ///// </summary>
        //public List<string> Rights
        //{
        //    get { return _Rights; }
        //    set { _Rights = value; }
        //}


        ///// <summary>
        ///// 当前用户构造函数
        ///// </summary>
        ///// <param name="usID">用户编码</param>
        ///// <param name="dataareaID">当前公司编码</param>
        ///// <param name="defaultCompanyID">默认公司编码</param>
        //public void Init(string usID, string dataareaID, string defaultCompanyID)
        //{
        //    this.UserID = usID;
        //    this.DATAAREAID = dataareaID;
        //    this.默认公司编码 = defaultCompanyID;
        //}

        //#region  BlueSky修改
        ///// <summary>
        ///// 定义语言编码（BlueSky）
        ///// </summary>
        //public string LanguageID { get; set; }

        ///// <summary>
        ///// 重载函数
        ///// </summary>
        ///// <param name="usID"></param>
        ///// <param name="dataareaID"></param>
        ///// <param name="defaultCompanyID"></param>
        ///// <param name="languageID"></param>
        //public void Init(string usID, string dataareaID, string defaultCompanyID, string languageID)
        //{
        //    this.UserID = usID;
        //    this.DATAAREAID = dataareaID;
        //    this.默认公司编码 = defaultCompanyID;
        //    this.LanguageID = languageID;
        //}
        //#endregion

        //#endregion

        #region 辅助方法
        private void GetConfigurationInfo()
        {
            if (FilePath != null && FilePath != "")
            {
                DataTable dTable = new DataTable("CurrentInfo");
                dTable.Columns.Add("Port", typeof(int));
                dTable.Columns.Add("IP", typeof(string));
                dTable.Columns.Add("Company", typeof(string));
                dTable.Columns.Add("User", typeof(string));
                dTable.Columns.Add("LocalFilePath", typeof(string));
                dTable.Columns.Add("DataBaseIP", typeof(string));
                FileInfo fInfo = new FileInfo(FilePath + "\\" + FileName);
                if (fInfo.Exists)
                {
                    dTable.ReadXml(FilePath + "\\" + FileName);
                    if (dTable.Rows.Count > 0)
                    {
                        this.ServerPort = Convert.ToInt32(dTable.Rows[0]["Port"]);
                        this.ServerIPAddress = dTable.Rows[0]["IP"].ToString();
                        this.DefaultUser = dTable.Rows[0]["User"].ToString();
                        this.DefaultCompany = dTable.Rows[0]["Company"].ToString();
                        this.DataBaseServerIP = dTable.Rows[0]["DataBaseIP"].ToString();
                    }
                }
            }
        }

        /// <summary>
        /// 生成配置文件的文件名（根据IP地址或者本机名）
        /// </summary>
        public static string GetCurrentFileName()
        {
            string name = "setup.xml";
            string localHostName = System.Net.Dns.GetHostName();
            System.Net.IPAddress[] ipAdds = System.Net.Dns.GetHostAddresses(localHostName);
            if (ipAdds.Length > 0)
                return ipAdds[0].ToString() + name;
            else
                return localHostName + name;
        }
        #endregion

    }

    //public enum RunState
    //{
    //    /// <summary>
    //    /// 用户在Form下面运行
    //    /// </summary>
    //    Form = 0,


    //    /// <summary>
    //    /// 用户在Web下面运行
    //    /// </summary>
    //    Web
    //}

    //public class FormUser : CurrentUser
    //{
    //    private static FormUser derivedInstance;
    //    public static FormUser GetInstance()
    //    {
    //        if (derivedInstance == null)
    //            derivedInstance = new FormUser();
    //        return derivedInstance;
    //    }
    //}

    //public class WebUser : CurrentUser
    //{
    //    public static WebUser GetInstance()
    //    {
    //        if (HttpContext.Current.Session == null || HttpContext.Current.Session["WebUser"] == null)
    //        {
    //            HttpContext.Current.Session["WebUser"] = new WebUser();
    //        }
    //        return HttpContext.Current.Session["WebUser"] as WebUser;
    //    }
    //}
}
